package com.cat.greedyAlgorithm;

import com.sun.security.auth.UnixNumericGroupPrincipal;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/transform-array-to-all-equal-elements/description/
 * @create 2025/8/25 11:15
 * @since JDK17
 */

public class Solution32 {
    boolean f(int[] nums, int k, int target) {
        int n = nums.length - 1;
        for (int i = 0; i < n; i++) {
            if (nums[i] != target) {
                nums[i + 1] = nums[i + 1] * -1;
                k--;
            }
            if (k < 0) {
                return false;
            }
        }
        return nums[n] == target;
    }

    public boolean canMakeEqual(int[] nums, int k) {
        int n = nums.length;
        int[] h = new int[n];
        for (int i = 0; i < n; i++) {
            h[i] = nums[i];
        }
        return f(h, k, 1) || f(nums, k, -1);
    }
}
